<template>
  <div class="detail">
    <!-- 顶部导航 -->

    <!-- 返回上一级 -->
    <van-nav-bar
      title="详情页"
      left-text="返回"
      right-text="..."
      left-arrow
      @click-left="$router.go(-1)" 
    />
    <!-- 店铺头部 -->
    <div class="shop-header" :style="{backgroundImage:'url(' + (shopinfo.info && shopinfo.info.poi_back_pic_url) +')' }">
      <div class="left">
        <img :src="shopinfo.info &&shopinfo.info.picUrl" alt="">
        <span>{{shopinfo.info &&shopinfo.info.name}}</span>
      </div>
      <!-- 取消收藏 -->
      <!-- <div v-if="isCollect" class="right" @click="$store.commit('cancel_collect',shopinfo.info)" style="color:red" >❤</div> -->
      <div v-if="isCollect" class="right" @click="request_unfav(shopinfo.info)" style="color:red" >❤</div>
      <!-- 收藏按钮 -->
      <div v-else class="right"  @click="request_fav(shopinfo.info)">❤</div>
    </div>

    <!-- 店铺内的 商品分类和列表 -->
    <div class="box">
      <div class="left">
        <van-sidebar v-model="currentIndex">
          <van-sidebar-item  v-for="(item,index) in shopinfo.taglist" :key="index" :title="item.name" />
        </van-sidebar>
      </div>
      <div class="right">
        <div class="good" v-for="(item,index) in  shopinfo.taglist &&shopinfo.taglist[currentIndex].goodlist" :key="index">
          <div class="imgbox">
            <img :src="item.picture" alt="">
          </div>
          <div class="text">
            <div class="up">
              <div class="name">{{item.name}}</div>
              <div class="desc">{{item.month_saled_content}}份 <span></span></div>
            </div>
            <div class="down">
              <div class="price">¥{{item.min_price}}</div>
              <!-- 添加到购物车 -->
              <div class="opes" >
                <span v-show="isInShopcart(item)" @click="request_shopcartsub(item)">-</span>
                <span v-show="isInShopcart(item)">{{isInShopcartCount(item)}}</span>
                <span @click="request_shopCartAdd(item)">+</span>  
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 购物车 -->
    <Shopcart ref="shopcart"></Shopcart>
    <div class="summary">
      <!-- 通过ref设置的标识 获取子组件实例，进而调用子组件方法 -->
      <span @click="$refs.shopcart.changeVisible()"><img src="https://gw.alicdn.com/imgextra/i2/O1CN01uGE6Bn1m4FQ1OiPfF_!!6000000004900-2-tps-249-249.png" alt=""></span>
      <div class="addprice">
        <span>￥{{price}}</span>
        <span>共有{{num}}件商品</span>

      </div>
      <span>去结算</span>
    </div>
  </div>
</template>

<script>
// 导入购物车组件
import Shopcart from './Shopcart/Shopcart.vue'
// 导入vuex中的工具函数(辅助函数)，使用该工具函数，简化vuex在组件中的应用
import {mapState,mapMutations,mapActions} from 'vuex'
// 导入组件库弹窗组件
import { Dialog } from 'vant';
// 导入封装好的函数
import { shop_shopinfo } from '../utils/api'
export default {
  data(){
    return {
      currentIndex:0, //根据下标来获取响应的数据
      shopinfo:{}, //存储获取到的数据，用来渲染列表
    }
  },

  computed:{// 计算方法
    //mapState()这个工具函数 会将vuex中的数据 映射到组件的计算属性中
    ...mapState(['collectlist','shopCartList']),
    isCollect(){//判断当前店铺已经被收藏 (true 表示已收藏 ，false 表示未收藏)
      // 拿到 当前店铺对象，还要同时拿到收藏列表，然后判断，返回判断结果
      // var index = this.$store.state.collectlist.findIndex((item)=>{
      var index = this.collectlist.findIndex((item)=>{
        return item.mtWmPoiId== this.shopinfo.mtWmPoiId;
      })
      return index !== -1;
    },
    
    price(){//计算购物车中的总价
      var num=0;
      for(var i=0;i<this.shopCartList.length;i++){
       num+= (this.shopCartList[i].min_price)*(this.shopCartList[i].count);
      }
      return num.toFixed(2);
    },
    num(){//计算购物车中的商品总数
      var sum=0;
      for(var i=0;i<this.shopCartList.length;i++){
        sum+= this.shopCartList[i].count;
      }
     return sum
    }
  },
  methods:{
    // ...mapMutations()这个工具函数 会将vuex中的数据 映射到组件的methods中
    ...mapMutations(['']),
    ...mapActions(['request_fav','request_unfav','request_shopCartAdd','request_shopcartsub']),

    isInShopcart(item){ //当前商品是否在购物车
      // item 是需要判断的商品对象
    var index =  this.shopCartList.findIndex((it)=>{
        return it.id == item.id
      })
      return index !==-1
    },
    
    isInShopcartCount(item){ //当前商品在购物车中的数量
      // item 是需要判断的商品对象
    var good  =  this.shopCartList.find((it)=>{
        return it.id == item.id
      })
      if(good){ //成立说明当前商品在
       return good.count;
      }
    },
  },
  mounted(){ //挂载函数，用来获取数据，只执行一次
    shop_shopinfo( {id: this.$route.params.id} ).then((res)=>{ //通过传过来的店铺id来发送请求获取相应的数据
      if(res.data.shopinfo){
        this.shopinfo = res.data.shopinfo;
      }else{
        Dialog.alert({
          title:'提示',
          message:'该店铺已倒闭',
        }).then(()=>{
          this.$router.go(-1);
        });
      }
    })
    
  },
  // 注册购物车组件
  components:{
    Shopcart,
  }
}
</script>

<style lang='scss' scoped>
.detail{
  padding-bottom: 50px;
}
.shop-header{
  display: flex;
  justify-content: space-between;
  padding: 50px 10px;
  background-size: cover;
  background-repeat: no-repeat;
  color: white;
  .left{
    flex:1;
    display: flex;
    align-items: center;
    img{
      width: 30px;
      margin-right: 10px;
      border-radius: 50%;
    }
  }
  .right{
    width: 20px;
    text-align: right;
  }
}
.box{
  display: flex;
  .left{
    width: 80px;
  }
  .right{
    flex:1;
    .good{
      display: flex;
      margin: 10px;
      line-height: 20px;
      .imgbox{
        width: 90px;
        margin-right: 10px;
        img{
          width: 100%;
        }
      }
      .text{
        flex:1;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .up{
          .name{
            font-size: 16px;
            font-weight: bold;

          }
        }
        .down{
          display: flex;
          justify-content: space-between;
          margin-top: 20px;
          .price{
            font-size: 16px;
            color: red;
          }
          .opes{
            span:nth-child(2){
              background: white;
              color: black;
            }
           
            span{
              width: 20px;
              height: 20px;
              background: rgb(72, 161, 239);
              border-radius: 50%;
              display: inline-block;
              font-size: 18px;
              color: white;
              text-align: center;
              line-height: 20px;
              margin-right: 5px;
            }
          }
        }
      }
    }
  }
}
.shopcart{
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  position: fixed;
  bottom: 0;
}
// 底部购物车按钮
.summary{
  position: fixed;
  bottom: 0;
  height: 45px;
  width: 100%;
  background:rgba(0, 0,0, 0.7);
  color: white;
  display: flex;
  justify-content: space-between;
  padding: 10px 10px ;
  box-sizing: border-box;
}
.summary span:nth-child(1) {
  display: inline-block;
  width: 60px;
  position: absolute;
  top: -25px;
  left: 0;
}
.summary span:nth-child(1) img{
  width: 100%;
}
.summary span:nth-child(2) {
  font-size: 12px;
  margin-left: 50px;
}
.summary span:nth-child(3){
  padding: 5px 20px;
  background: rgb(11, 200, 52);
  border-radius: 5px;
  position: absolute;
  top:10%;
  right: 5px;
}
.summary .addprice{
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
.summary .addprice span:nth-child(1){
  top: 3px;
  left: 60px;
  font-size: 14px;
}
.summary .addprice span:nth-child(2){
  margin-top: 10px;
}
</style>